package com.kotiln.lgc.lib_network.javahttp;

import android.os.AsyncTask;

import org.json.JSONException;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

public abstract class BaseHttpRequest extends AsyncTask<Void, Void, BaseHttpRequest.Result> {
	
	protected String mUrl;
	
	protected String mTradeType;
	
	public BaseHttpRequest(String url){
		this(url,"GET");
	}
	
	public BaseHttpRequest(String url,String tradeType){
		this.mUrl = url;
		this.mTradeType = tradeType;
	}
	
	@Override
	protected Result doInBackground(Void...params) {
		URL realurl = null;
		InputStream in = null;
		HttpURLConnection conn = null;
		StringBuffer sBuffer = null;
		
		try {
			realurl = new URL(mUrl);
			conn = (HttpURLConnection) realurl.openConnection();
			conn.setDoInput(true); 
			conn.setReadTimeout(10000);
			conn.setConnectTimeout(15000);
			conn.setRequestMethod(mTradeType);
			if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
				sBuffer = new StringBuffer();
			    byte[] buf = new byte[1024];  
			    in = conn.getInputStream();
			    for (int n; (n = in.read(buf)) != -1;) {  
			        sBuffer.append(new String(buf, 0, n, "gbk"));  
			    }  
			}else{
				return new Result(-1, "read error");
			}
			
		} catch (MalformedURLException e) {
			return new Result(-90, "MalformedURLException");
		} catch (ProtocolException e) {
			return new Result(-91, "ProtocolException");
		} catch (UnsupportedEncodingException e) {
			return new Result(-92, "UnsupportedEncodingException");
		} catch(SocketTimeoutException e){
			return new Result(-93, "SocketTimeoutException");
		}catch (UnknownHostException  e) {
			return new Result(-95, "UnknownHostException");
		}catch (IOException e) {
			return new Result(-96, "IOException");
		}
		finally{
			try {
				if(in != null) in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
			if(conn != null) conn.disconnect();
		}
		
		return new Result(0, sBuffer.toString());
	}
	
	@Override
	protected void onPostExecute(Result result) {
		super.onPostExecute(result);
		try {
			afterTrade(result);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	protected abstract void afterTrade(Result result) throws JSONException;

	public class Result {
		int resCode; // 0,成功
		String message;

		Result(int resCode, String errMessage) {
			this.resCode = resCode;
			this.message = errMessage;
		}

		public int getResCode() {
			return resCode;
		}

		public void setResCode(int resCode) {
			this.resCode = resCode;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		@Override
		public String toString() {
			return "resCode = " + resCode + " ,message = " + message;
		}
	}

}
